﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using BuyWithoutLoseMoney.Core.Models;

namespace BuyWithoutLoseMoney
{
    public class BuyWithoutLoseMoneyDBContext : DbContext
    {
        public BuyWithoutLoseMoneyDBContext(DbContextOptions<BuyWithoutLoseMoneyDBContext> options): base(options)
        {

        }
        public DbSet<User> Users { get; set; }
        public DbSet<UserType> userTypes { get; set; }
        public DbSet<UserAddress> UserAddresses { get; set; }
        public DbSet<AboutUS> AboutUs { get; set; }
        public DbSet<Order> Order { get; set; }
        public DbSet<OrderDetails> OrderDetails { get; set; }
        public DbSet<LoginUser> LoginUsers { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<Permission> Permissions { get; set; }
        public DbSet<PermissionType> PermissionTypes { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<ProductType> ProductTypes { get; set; }
        public DbSet<ProductPic> ProductPic { get; set; }
        public DbSet<Comment> Comment { get; set; }
        public DbSet<News> News { get; set; }
        public DbSet<NewsType> NewsTypes { get; set; }
        public DbSet<Position> Positions { get; set; }
        public DbSet<ResPosition> ResPositions { get; set; }
        public DbSet<PermissionRole> PermissionRoles { get; set; }
        public DbSet<LoginUserRole> LoginUserRoles { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Role>()
                .HasMany(e => e.Permission)
                .WithMany(e => e.Roles)
                .UsingEntity<PermissionRole>(
                 l => l.HasOne<Permission>(e => e.Permission).WithMany(e => e.RolePermission),
            r => r.HasOne<Role>(e => e.Role).WithMany(e => e.RolePermission));
            modelBuilder.Entity<LoginUser>()
               .HasMany(e => e.Role)
               .WithMany(e => e.LoginUser)
               .UsingEntity<LoginUserRole>(
                l => l.HasOne<Role>(e => e.Role).WithMany(e => e.LoginUserRoles),
           r => r.HasOne<LoginUser>(e => e.LoginUser).WithMany(e => e.LoginUserRoles));
        }
    }
}
